import { useState } from "react";
import {
  Input,
  Button,
  Card,
  Row,
  Col,
  Statistic,
  Switch,
  message,
  Divider,
} from "antd";
import {
  CopyOutlined,
  DeleteOutlined,
  LineChartOutlined,
} from "@ant-design/icons";
import { textToolsData } from "@/components/ToolsData";
import ToolTitle from "@/components/ToolTitle";
const { TextArea } = Input;

const TextDeduplication = () => {
  const [inputText, setInputText] = useState("");
  const [outputText, setOutputText] = useState("");
  const [keepEmpty, setKeepEmpty] = useState(true);
  const [caseSensitive, setCaseSensitive] = useState(false);
  const [stats, setStats] = useState({ total: 0, unique: 0, duplicates: 0 });

  const processText = () => {
    if (!inputText.trim()) {
      message.warning("请输入需要去重的文本");
      return;
    }

    const lines = inputText.split("\n");
    const uniqueLines = new Set<string>();
    const processedLines: string[] = [];

    let emptyLineCount = 0;
    let duplicateCount = 0;

    lines.forEach((line) => {
      const processedLine = caseSensitive
        ? line.trim()
        : line.trim().toLowerCase();
      const isEmpty = line.trim() === "";

      // 空行处理
      if (isEmpty) {
        if (keepEmpty) {
          processedLines.push(line);
          uniqueLines.add("__EMPTY_LINE__");
        } else {
          emptyLineCount++;
        }
        return;
      }

      // 重复性检查
      if (uniqueLines.has(processedLine)) {
        duplicateCount++;
        return;
      }

      uniqueLines.add(processedLine);
      processedLines.push(line);
    });

    setOutputText(processedLines.join("\n"));
    setStats({
      total: lines.length,
      unique: uniqueLines.size - (keepEmpty ? 0 : emptyLineCount),
      duplicates: duplicateCount,
    });
  };

  const copyResult = () => {
    if (outputText) {
      navigator.clipboard.writeText(outputText);
      message.success("已复制到剪贴板");
    }
  };

  const clearAll = () => {
    setInputText("");
    setOutputText("");
    setStats({ total: 0, unique: 0, duplicates: 0 });
  };

  return (
    <div className="p-4">
      <Card bordered={false} className="shadow-md">
        <ToolTitle
          title={
            textToolsData.find((item) => item.title === "文本去重")?.title || ""
          }
          description={
            textToolsData.find((item) => item.title === "文本去重")
              ?.description || ""
          }
        />

        <Divider />

        <Row gutter={16}>
          <Col span={12}>
            <TextArea
              rows={12}
              value={inputText}
              onChange={(e) => setInputText(e.target.value)}
              placeholder="请输入需要去重的文本，支持多行文本处理..."
              style={{ marginBottom: 16 }}
            />
          </Col>
          <Col span={12}>
            <TextArea
              rows={12}
              value={outputText}
              readOnly
              placeholder="去重结果将显示在这里"
              style={{ marginBottom: 16, backgroundColor: "#fafafa" }}
            />
          </Col>
        </Row>

        <Row gutter={16} className="mb-4">
          <Col>
            <Button
              type="primary"
              onClick={processText}
              icon={<LineChartOutlined />}
            >
              开始去重
            </Button>
          </Col>
          <Col>
            <Button onClick={copyResult} icon={<CopyOutlined />}>
              复制结果
            </Button>
          </Col>
          <Col>
            <Button danger onClick={clearAll} icon={<DeleteOutlined />}>
              清空全部
            </Button>
          </Col>
        </Row>

        <Row gutter={16} className="mb-4">
          <Col>
            <Switch
              checked={keepEmpty}
              onChange={(checked) => setKeepEmpty(checked)}
              checkedChildren="保留空行"
              unCheckedChildren="删除空行"
            />
          </Col>
          <Col>
            <Switch
              checked={caseSensitive}
              onChange={(checked) => setCaseSensitive(checked)}
              checkedChildren="区分大小写"
              unCheckedChildren="忽略大小写"
            />
          </Col>
        </Row>

        <Row gutter={16}>
          <Col span={8}>
            <Statistic title="总行数" value={stats.total} />
          </Col>
          <Col span={8}>
            <Statistic title="去重行数" value={stats.unique} />
          </Col>
          <Col span={8}>
            <Statistic title="重复行数" value={stats.duplicates} />
          </Col>
        </Row>
      </Card>
    </div>
  );
};

export default TextDeduplication;
